#include "wx/wxprec.h"

#include "AUIDocViewChildFrame.h"
#include "AUIDocViewMainFrame.h"

#include "../MyGLCanvas.h"

#include "news_sticky.xpm"
#include "news_question.xpm"

enum
{
	ID_CHILD_TOGGLE_TOOLBAR = 11001,
	ID_CHILD_TOGGLE_TREECTRL,
	//--- BUTTONS ---
	ID_CHILD_BUTTON_ID_START = 12000
};

BEGIN_EVENT_TABLE(AUIDocViewChildFrame, wxAuiMDIChildFrame)
EVT_ACTIVATE(AUIDocViewChildFrame::OnActivated)
END_EVENT_TABLE()

AUIDocViewChildFrame::AUIDocViewChildFrame()
{
}

bool AUIDocViewChildFrame::Create(wxAuiMDIParentFrame * parent, wxWindowID id, 
								  const wxString & title, bool has_toolbar)
{	
	bool res = wxAuiMDIChildFrame::Create(parent, id, title);
	if(res)
	{
		m_ToolBar = NULL;
		m_HasToolBar = has_toolbar;
		CreateControls();
	}
	return res;
}

void AUIDocViewChildFrame::CreateControls()
{
	SetSizer(new wxBoxSizer(wxVERTICAL));
	m_ToolBar = CreateToolBar(wxTB_FLAT|wxTB_NODIVIDER, wxID_ANY, GetTitle());
	if(m_ToolBar)
	{
		GetSizer()->Add(m_ToolBar, 0, wxGROW);
		Connect(ID_CHILD_TOGGLE_TOOLBAR,
				wxEVT_COMMAND_MENU_SELECTED,
				wxCommandEventHandler(AUIDocViewChildFrame::OnToggleToolbar));
		Connect(ID_CHILD_TOGGLE_TOOLBAR,
				wxEVT_UPDATE_UI,
				wxUpdateUIEventHandler(AUIDocViewChildFrame::OnToggleToolbarUpdateUI));
	}
	SetMenuBar(CreateMenuBar());
	SetBackgroundColour(wxColour(100+rand()%156, 100+rand()%156, 100+rand()%156));
}

wxMenuBar * AUIDocViewChildFrame::CreateMenuBar()
{
	wxMenuBar * menuBar = new wxMenuBar;
	
	wxMenu * fileMenu = new wxMenu;
	fileMenu->Append(wxID_EXIT, _("Exit\tAlt+F4"));
	menuBar->Append(fileMenu, _("File"));

	wxMenu * viewMenu = new wxMenu;	
	wxMenu * headerMenu = new wxMenu;
	headerMenu->AppendRadioItem(ID_HEADER_DEFAULT_STYLE, _("Default"));
	headerMenu->AppendRadioItem(ID_HEADER_SIMPLE_STYLE, _("Simple"));
	viewMenu->Append(wxID_ANY, _("Header"), headerMenu);
	viewMenu->AppendCheckItem(ID_TOGGLE_LOGWINDOW, _("Toggle Help Window"));
	if(m_HasToolBar)
	{		
		viewMenu->AppendCheckItem(ID_CHILD_TOGGLE_TOOLBAR, _("Toggle toolbar"));			
	}
	menuBar->Append(viewMenu, _("View"));
	
	wxMenu * helpMenu = new wxMenu;
	helpMenu->Append(wxID_ABOUT, _("About..."));
	menuBar->Append(helpMenu, _("Help"));

	return menuBar;
}

void AUIDocViewChildFrame::OnToggleToolbar(wxCommandEvent & event)
{
	if(m_ToolBar)
	{
		if(m_ToolBar->IsShown())
		{
			GetSizer()->Detach(m_ToolBar);
			m_ToolBar->Hide();
		}
		else
		{
			m_ToolBar->Show();
			GetSizer()->Insert(0, m_ToolBar, 0, wxGROW);
		}
	}
}

void AUIDocViewChildFrame::OnToggleToolbarUpdateUI(wxUpdateUIEvent & event)
{
	if(m_ToolBar)
	{
		event.Check(m_ToolBar->IsShown());
	}
	else event.Skip();
}

wxToolBar * AUIDocViewChildFrame::CreateToolBar(long style,
												wxWindowID winid,
												const wxString& WXUNUSED(name))
{
	if(!m_ToolBar && m_HasToolBar)
	{
		m_ToolBar = new wxToolBar(this, winid, wxDefaultPosition, wxDefaultSize, style);
		wxBitmap bmp;
		for(int i = 0; i < 1+rand()%10; i++)
		{
			if(rand()%2)
			{
				bmp = wxBitmap(news_sticky_xpm);
			} else bmp = wxBitmap(news_question_xpm);
			wxWindowID id = ID_CHILD_BUTTON_ID_START+i;
			m_ToolBar->AddTool(id, bmp, wxString::Format(wxT("Tool %i"), i));
			Connect(id, wxEVT_COMMAND_MENU_SELECTED,
					wxCommandEventHandler(AUIDocViewChildFrame::OnToolButton));
		}
		m_ToolBar->Realize();
	}
	return m_ToolBar;
}
						
wxToolBar * AUIDocViewChildFrame::GetToolBar() const
{
	return m_ToolBar;
}

void AUIDocViewChildFrame::OnToolButton(wxCommandEvent & event)
{
	wxMessageBox(wxString::Format(_("Button %u pressed"), 
		event.GetId()-ID_CHILD_BUTTON_ID_START+1));
}

void AUIDocViewChildFrame::OnActivated(wxActivateEvent & event)
{	
	AUIDocViewMainFrame * frame = wxDynamicCast(GetMDIParentFrame(), AUIDocViewMainFrame);
	if(frame)
	{
		frame->GetLOGTextCtrl()->SetValue(wxString::Format(
			_("Some help text about '%s'"),	GetTitle().GetData()));
	}
}

BEGIN_EVENT_TABLE(AUIManagedChildFrame, AUIDocViewChildFrame)
EVT_MENU(ID_CHILD_TOGGLE_TREECTRL, AUIManagedChildFrame::OnToggleTreeCtrl)
EVT_UPDATE_UI(ID_CHILD_TOGGLE_TREECTRL, AUIManagedChildFrame::OnToggleTreeCtrlUpdateUI)
END_EVENT_TABLE()

AUIManagedChildFrame::~AUIManagedChildFrame()
{
	m_FrameManager.UnInit();
}

void AUIManagedChildFrame::CreateControls()
{

	AUIDocViewChildFrame::CreateControls();

	m_FrameManager.SetManagedWindow(this);	

	
	//wxPanel * panel = new wxPanel(this, wxID_ANY);
	//panel->SetBackgroundColour(
	//	wxColour(100+rand()%156, 100+rand()%156, 100+rand()%156));
	//	m_FrameManager.AddPane(panel, 
	//	wxAuiPaneInfo().Name(wxT("CenterPane")).CenterPane());
		
	m_FrameManager.AddPane(new MyGLCanvas (this)/*panel*/, 
		wxAuiPaneInfo().Name(wxT("CenterPane")).CenterPane());
	
	if (!GetMenuBar() )
		SetMenuBar(CreateMenuBar());	
	
	//return;
	wxToolBar * toolBar = CreateToolBar(wxTB_FLAT|wxTB_NODIVIDER, wxID_ANY, GetTitle());
	if(toolBar)
	{
		m_FrameManager.AddPane(toolBar, wxAuiPaneInfo().Name(wxT("ToolBar")).
			ToolbarPane().Top());
		Connect(ID_CHILD_TOGGLE_TOOLBAR,
				wxEVT_COMMAND_MENU_SELECTED,
				wxCommandEventHandler(AUIManagedChildFrame::OnToggleToolbar));
		Connect(ID_CHILD_TOGGLE_TOOLBAR,
				wxEVT_UPDATE_UI,
				wxUpdateUIEventHandler(AUIManagedChildFrame::OnToggleToolbarUpdateUI));
	}	
			
	m_TreeCtrl = new wxTreeCtrl(this, wxID_ANY, wxDefaultPosition,
		wxSize(150, 300));
	wxTreeItemId root = m_TreeCtrl->AddRoot(_("Root"));
	m_TreeCtrl->AppendItem(root, _("Node 1"));
	m_TreeCtrl->AppendItem(root, _("Node 2"));
	m_TreeCtrl->AppendItem(root, _("Node 3"));
	m_TreeCtrl->Expand(root);	
	m_FrameManager.AddPane(m_TreeCtrl, wxAuiPaneInfo().Name(wxT("Tree")).Left());	
	m_FrameManager.Update();		
}

void AUIManagedChildFrame::OnToggleToolbar(wxCommandEvent & event)
{
	TogglePaneVisibility(wxT("ToolBar"));
}

void AUIManagedChildFrame::OnToggleToolbarUpdateUI(wxUpdateUIEvent & event)
{
	event.Check(GetPaneVisibility(wxT("ToolBar")));
}

void AUIManagedChildFrame::OnToggleTreeCtrl(wxCommandEvent & event)
{
	TogglePaneVisibility(wxT("Tree"));
}

void AUIManagedChildFrame::OnToggleTreeCtrlUpdateUI(wxUpdateUIEvent & event)
{
	event.Check(GetPaneVisibility(wxT("Tree")));
}

bool AUIManagedChildFrame::GetPaneVisibility(wxString pane_name)
{
	wxAuiPaneInfoArray& all_panes = m_FrameManager.GetAllPanes();
	size_t i, count;
	for (i = 0, count = all_panes.GetCount(); i < count; ++i)
	{		
		if(all_panes.Item(i).name == pane_name)
		{
				return all_panes.Item(i).IsShown();
		}
	}
	return false;
}

void AUIManagedChildFrame::TogglePaneVisibility(wxString pane_name)
{
	wxAuiPaneInfoArray& all_panes = m_FrameManager.GetAllPanes();
	size_t i, count;
	for (i = 0, count = all_panes.GetCount(); i < count; ++i)
	{		
		if(all_panes.Item(i).name == pane_name)
		{
			all_panes.Item(i).Show(!all_panes.Item(i).IsShown());
			m_FrameManager.Update();
			break;
		}
	}
}

wxMenuBar * AUIManagedChildFrame::CreateMenuBar()
{
	wxMenuBar * menuBar;
	menuBar = new wxMenuBar;
	
	wxMenu * fileMenu = new wxMenu;
	fileMenu->Append(wxID_EXIT, _("Exit\tAlt+F4"));
	menuBar->Append(fileMenu, _("File"));

	wxMenu * viewMenu = new wxMenu;	
	wxMenu * headerMenu = new wxMenu;
	headerMenu->AppendRadioItem(ID_HEADER_DEFAULT_STYLE, _("Default"));
	headerMenu->AppendRadioItem(ID_HEADER_SIMPLE_STYLE, _("Simple"));
	viewMenu->Append(wxID_ANY, _("Header"), headerMenu);
	viewMenu->AppendCheckItem(ID_TOGGLE_LOGWINDOW, _("Toggle Help Window"));
	viewMenu->AppendCheckItem(ID_CHILD_TOGGLE_TREECTRL, _("Toggle tree"));
	if(m_HasToolBar)
	{		
		viewMenu->AppendCheckItem(ID_CHILD_TOGGLE_TOOLBAR, _("Toggle toolbar"));
	}
	menuBar->Append(viewMenu, _("View"));

	wxMenu * helpMenu = new wxMenu;
	helpMenu->Append(wxID_ABOUT, _("About..."));
	menuBar->Append(helpMenu, _("Help"));

	return menuBar;
}

//bool AUIManagedChildFrame::Create(wxAuiMDIParentFrame * parent, wxWindowID id, 
//								  const wxString & title, bool has_toolbar)
//{	
//	bool res = wxAuiMDIChildFrame::Create(parent, id, title);
//	if(res)
//	{
//		m_ToolBar = NULL;
//		m_HasToolBar = has_toolbar;
//		CreateControls();
//	}
//	return res;
//}